package com.kawaiior.asoulworld.util;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import scala.Int;

import javax.annotation.Nullable;
import java.util.function.Predicate;

public class AsoulWorldUtils {

    public static boolean consumeInventoryItem(EntityLivingBase living, Predicate<ItemStack> matcher, int count) {

        IItemHandler inv = living.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (inv == null)
        {
            return false;
        }

        boolean consumedSome = false;

        for (int i = 0; i < inv.getSlots() && count > 0; i++) {
            ItemStack stack = inv.getStackInSlot(i);
            if (matcher.test(stack)) {
                ItemStack consumed = inv.extractItem(i, count, false);
                count -= consumed.getCount();
                consumedSome = true;
            }
        }

        return consumedSome;
    }

    public static boolean seekInventoryItem(EntityLivingBase living, Predicate<ItemStack> matcher) {

        IItemHandler inv = living.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (inv == null)
        {
            return false;
        }
        for (int i = 0; i < inv.getSlots(); i++) {
            ItemStack stack = inv.getStackInSlot(i);
            if (matcher.test(stack)) {
                return true;
            }
        }

        return false;
    }

    @Nullable
    public static ItemStack getwhichItemStack(EntityPlayer player, Predicate<ItemStack> matcher)
    {
        IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (inv == null) {
            return null;
        }

        for (int i = 0; i < inv.getSlots(); i++) {
            ItemStack stack = inv.getStackInSlot(i);
            if (matcher.test(stack)) {
                return stack;
            }
        }
        return null;
    }

    @Nullable
    public static ItemStack getwhichItemStack2(EntityPlayer player, Predicate<ItemStack> matcher)
    {
        IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (inv == null) {
            return null;
        }

        for (int i = 0; i < 8; i++) {
            ItemStack stack = inv.getStackInSlot(i);
            if (matcher.test(stack)) {
                return stack;
            }
        }
        return null;
    }

    public static boolean getwhichItemStack3(EntityPlayer player, Predicate<ItemStack> matcher)
    {
        IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (inv == null) {
            return false;
        }

        for (int i = 0; i < 8; i++) {
            ItemStack stack = inv.getStackInSlot(i);
            if (matcher.test(stack)) {
                return true;
            }
        }
        return false;
    }


}
